package Search;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import Classes.Path;
import Classes.Query;
import PreProcessData.StopWordRemover;
import PreProcessData.WordNormalizer;
import PreProcessData.WordTokenizer;

/**
 * NOTE: The query content of each topic should be 
 * 1) tokenized, 
 * 2) lowercase, 
 * 3) remove stop words, 
 * 4) stemming
 * Normalization: lowercase + stemming
 * 
 * NOTE: You can simply pick up title only for query, or you can 
 * also use title + description + narrative for the query content.
 * 
 * 4 combinations:
 * 1) title only
 * 2) description only
 * 3) title and description 
 * 4) all of them
 * 
 * Remember that queries in general have multiple query terms.
 * 
 * NOTE: The forth topic contains a word "Dysphagia" that did not 
 * appear in the collection. You should detect and process such cases.
 * 
 * @author florahan
 */
public class ExtractQuery {

	private BufferedReader br;
//	private BufferedReader brReader;
	private String line;
//	private BufferedReader countQueryBr;
//	private int totalCountOfQueries; // Count total number of queries
//	private int countOfQueries; // Count the current number of queries 

	/**
	 * Extract the 4 queries from the Path.TopicDir.
	 * @throws IOException 
	 */
	public ExtractQuery() throws IOException {
		FileInputStream fis = new FileInputStream(Path.TopicDir);
		InputStreamReader isr = new InputStreamReader(fis);
//		processQueryBr = new BufferedReader(isr);
		br = new BufferedReader(isr);
//		brReader = new BufferedReader(isr);
		
//		countQueryBr = new BufferedReader(isr);
//		String line = "";
//		while((line = countQueryBr.readLine()) != null) {
//			if(line.contains("<top>")) {
//				totalCountOfQueries++;
//			}
//		}
	}
	
	/**
	 * Check if there is still next line in topics.txt file.
	 * Return false when reaching the end of the file.
	 * @return
	 * @throws IOException 
	 */
	public boolean hasNext() throws IOException {
		if((line = br.readLine()) != null) {
			return true;
		} else {
			br.close();
		}
//		if(countOfQueries <= totalCountOfQueries) {
//			return true;
//		}
//		if ((line = brReader.readLine()) != null)
//            return true;
//        if (brReader != null)
//            brReader.close();
		return false;
	}
	
	/**
	 * Obtain the current query.
	 * @return
	 * @throws IOException 
	 */
	public Query next() throws IOException {
		
		Query currentQuery = new Query();
		
		//1. Obtain query/topic id, title, desc, narr
		String title = "", desc = "", narr = "";
		while((line = br.readLine()) != null) {
			if(line.contains("<num>")) {
				currentQuery.SetTopicId(line.substring(line.indexOf(":") + 2, line.length()));
			}
			
			if(line.contains("<title>")) {
				title = line.substring(line.indexOf(">") + 2, line.length());
			}
			
			if(line.contains("<desc>")) {
				while(!(line = br.readLine()).contains("<narr>")) {
					desc += line;
				}
			}
			
			if(line.contains("<narr>")) {
				while(!(line = br.readLine()).contains("</top>")) {
					narr += line;
				}
			}	
			
			if(line.contains("</top>")) {
				//2. Tokenization & Normalization & Remove stop words
				WordTokenizer titleTokenizer = new WordTokenizer(title.toCharArray()); // Get each word as a token in title
				WordTokenizer descTokenizer = new WordTokenizer(desc.toCharArray());   // Get each word as a token in desc
				WordTokenizer narrTokenizer = new WordTokenizer(narr.toCharArray());   // Get each word as a token in narr
				StopWordRemover stopWordRemover = new StopWordRemover();
				WordNormalizer wordNormalizer = new WordNormalizer();
				String normTitle = "", normDesc = "", normNarr = "";
				
				char[] word = {};
				while((word = titleTokenizer.nextWord()) != null) {
					word = wordNormalizer.lowercase(word);
			        if (!stopWordRemover.isStopword(word))
			          normTitle += wordNormalizer.stem(word) + " ";
				}
				
				while((word = descTokenizer.nextWord()) != null) {
					word = wordNormalizer.lowercase(word);
			        if (!stopWordRemover.isStopword(word))
			          normDesc += wordNormalizer.stem(word) + " ";
				}
				
				while((word = narrTokenizer.nextWord()) != null) {
					word = wordNormalizer.lowercase(word);
			        if (!stopWordRemover.isStopword(word))
			          normNarr += wordNormalizer.stem(word) + " ";
				}
				
				// 3. Set query
				currentQuery.setQueryTitle(normTitle);
				currentQuery.setQueryDesc(normDesc);
				currentQuery.setQueryNarr(normNarr);
				
				// 4. Choose title as query content
				currentQuery.SetQueryContent(normTitle);
				return currentQuery;
			}
		} // End of while loop
//		countOfQueries++;
		return null;
	}
	
	public static void main(String[] args) throws IOException {
		ExtractQuery queries = new ExtractQuery();
//		System.out.println(queries.totalCountOfQueries);
		while(queries.hasNext()) {
			Query aQuery = queries.next();
			System.out.println("Query: " + aQuery);
//			System.out.println(queries.countOfQueries);
			System.out.println(aQuery.GetTopicId() + "\t" + aQuery.GetQueryContent());
		}
	}
}